﻿// Copyright @ shch8.com All Rights Reserved At 2008-3-6
//开发：商创技术（www.shch8.com）望月狼
/*
·数字计算数学公式处理
 可直接按参数直接调用

 例：strUtil.revise(8)//返回"08"

 */
package com._public._utils{
	import flash.geom.Point;
	public class MathUtil {
		/**
		 * 图片最大尺寸限制
		 * @param	oldWid
		 * @param	oldHei
		 * @param	limitWid
		 * @param	limitHei
		 * @param	toMin		以最小的为基准
		 * @return
		 */
		public static function limitSize(oldWid:Number,oldHei:Number,limitWid:Number,limitHei:Number,toMin:Boolean=true):Point{
			if(oldWid<limitWid&&oldHei<limitHei)return new Point(oldWid,oldHei);
			var scale:Number=oldWid/oldHei
			if(toMin?(oldWid<=oldHei):(oldWid>=oldHei)){
				oldWid=limitWid
				oldHei=oldWid/scale
				}else{
					oldHei=limitHei
					oldWid=limitHei*scale
					}
			return new Point(oldWid,oldHei);
		}
		/*小于10的数字前加0处理*/
		public static function revise(inNum:*):String {
			var num:Number=parseInt(inNum);
			var strNum:String='0';
			if (num < 10 ) {
				strNum="0" + num;
			}else{
				strNum=String(num)
				}
			return strNum;
		}
		/**
		 * 计算两点间距
		 * @param	x1 起点X坐标
		 * @param	x2 终点X坐标
		 * @param	y1 起点Y坐标
		 * @param	y2 终点Y坐标
		 * @return 返回长度
		 */
		public static function pointBetween(x1:Number,x2:Number,y1:Number=0,y2:Number=0):Number {
			var a:Number=x2 - x1;
			var b:Number=y2 - y1;
			if (a < 0) {
				a*= -1;
			}
			if (b < 0) {
				b*= -1;
			}
			var c:Number=Math.sqrt(Math.pow(a,2) + Math.pow(b,2));//Math.Pow()->计算次方
			return c;
		}
		/*计算两个时间点的差距*/
		public static function timeBetween(timeFrom:Date,timeTo:Date):Array {
			/*var second_f:Number=timeFrom[0] * 3600 + timeFrom[1] * 60;
			var second_t:Number=timeTo[0] * 3600 + timeTo[1] * 60;
			if (timeFrom.length == 3 && timeTo.length == 3) {
				second_f+= timeFrom[2];
				second_t+= timeTo[2];
			}*/
			//var betweenT:Number = second_t - second_f;
			var betweenT:int = int((timeTo.getTime() - timeFrom.getTime())/1000/3600 / 24);//差距日
			var ary:Array=new Array(int(betweenT / 3600),int(betweenT % 3600 / 60),betweenT);
			return ary;
		}
		/*计算两点夹角*/
		public static function pointAngle(x1:Number,x2:Number,y1:Number=0,y2:Number=0,getXAngle:Boolean=true):Number {
			var lineA:Number=pointBetween(x1,x2,y1,y2);//getXAngle=true //获取水平夹角，获取垂直夹角 ，顺时针计算
			var usePoint:Number;
			var degrees:Number=180/Math.PI;
			var deg:Number;
			usePoint=pointBetween(0,0,y1,y2);
			deg=Math.asin(usePoint/lineA) * degrees;
			if (x1<=x2 && y1>y2) {//第一象限
				if (getXAngle) {//水平夹角
					return deg;
				} else {//垂直夹角
					return 270 + deg;
				}
			} else if (x1>x2 && y1>=y2) {//第二象限
				if (getXAngle) {
					return 180 - deg;
				} else {
					return 90 - deg;
				}
			} else if (x1>=x2 && y1<y2) {//第三象限
				if (getXAngle) {
					return deg + 180;
				} else {
					return 90 + deg;
				}
			} else if (x1<x2 && y1<=y2) {//第四象限
				if (getXAngle) {
					return 360 - deg;
				} else {
					return 270-deg;
				}
			} else {
				return deg;
			}
		}
		/*计算两个时间的和,参数：起始时间 时间长度*/
		public static function timeAdd(timeSta:Array,timeLen:Array):Array {
			var second_s:Number=timeSta[0] * 3600 + timeSta[1] * 60;
			var second_l:Number=timeLen[0] * 3600 + timeLen[1] * 60;
			if (timeSta.length == 3 && timeLen.length == 3) {
				second_s+= timeSta[2];
				second_l+= timeLen[2];
			}
			var betweenT:Number=Math.abs(second_s + second_l);
			var ary:Array=new Array(int(betweenT / 3600),int(betweenT % 3600 / 60),betweenT % 3600 % 60);
			return ary;
		}
		/*星期汉字转化0-6 0为周日*/
		public static function setWeek(day:Number):String {
			var chineseDay:Array=["星期日","星期一","星期二","星期三","星期四","星期五","星期六",];
			return chineseDay[day];
		}
	}
}